package com.tstudy.laoshibang.weike;

import java.util.List;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.manager.SearchHistoryManager;
import com.tstudy.laoshibang.mode.SearchHistory;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
/**
 * 搜索历史页adapter
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年6月3日 上午11:24:26
 */
public class SearchHistoryAdapter extends BaseAdapter{
	LayoutInflater mInflater;
	private List<SearchHistory> mItems;
	int mDrawablePadding = CommonUtil.dip2px(10);
	String clearSearchStr = BaseApplication.getResString(R.string.search_history_clear);
	Drawable itemLeftIcon = BaseApplication.mContext.getResources().getDrawable(R.drawable.icon_search_lit);
	Drawable itemRightIcon = BaseApplication.mContext.getResources().getDrawable(R.drawable.go_topleft);
	Drawable clearLeftIcon = BaseApplication.mContext.getResources().getDrawable(R.drawable.icon_empty);
	String viewCountStr = BaseApplication.getResString(R.string.active_list_view_count);
	public SearchHistoryAdapter() {
		 mInflater = LayoutInflater.from(BaseApplication.mContext);
	}
	
	@Override
	public int getCount() {
		return mItems == null ? 0:mItems.size();
	}
	
	public void setData(List<SearchHistory> items) {
		setData(items,true);
	}
	public void setData(List<SearchHistory> items,boolean isRefresh) {
		mItems = items;
		if (isRefresh) {
			notifyDataSetChanged();
		}
	}
	
	public List<SearchHistory> getData() {
		return mItems;
	}
	

	@Override
	public Object getItem(int position) {
		return mItems == null ? null:mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if(convertView ==  null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.search_history_item, null);
			viewHolder.name = (TextView) convertView.findViewById(R.id.search_history_item_name);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(viewHolder != null){
			setConvertView(viewHolder,position,convertView);
		}
//		convertView.setOnClickListener(null);
		return convertView;
	}
	
	public void setConvertView(final ViewHolder viewHolder,final int position,View view){
		final SearchHistory searchHistory = (SearchHistory) getItem(position);
		viewHolder.name.setText(searchHistory.name);
		RelativeLayout.LayoutParams nameLp = (LayoutParams) viewHolder.name.getLayoutParams();
		if (clearSearchStr.equals(searchHistory.name)) {
			nameLp.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
			clearLeftIcon.setBounds(0, 0, clearLeftIcon.getMinimumWidth(), clearLeftIcon.getMinimumHeight());
			viewHolder.name.setCompoundDrawables(clearLeftIcon, null,null, null);
			viewHolder.name.setGravity(Gravity.CENTER);
		}else{
			itemLeftIcon.setBounds(0, 0, itemLeftIcon.getMinimumWidth(), itemLeftIcon.getMinimumHeight());
			itemRightIcon.setBounds(0, 0, itemRightIcon.getMinimumWidth(), itemRightIcon.getMinimumHeight());
			viewHolder.name.setCompoundDrawables(itemLeftIcon, null,itemRightIcon, null);
			nameLp.width = RelativeLayout.LayoutParams.MATCH_PARENT;
			viewHolder.name.setCompoundDrawablePadding(mDrawablePadding);
			viewHolder.name.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
		}
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (clearSearchStr.equals(searchHistory.name)) {
					mItems.clear();
					setData(mItems);
					SearchHistoryManager.getInstance().clearByType(SearchHistory.class,searchHistory.type);
				}else{
					if (mActionCallBack != null) {
						mActionCallBack.onViewClick(searchHistory.name);
					}
				}
			}
		});
	}
	
	static class ViewHolder{
		TextView name;
	}
	
	public interface AdapterCallBack{
		public void onViewClick(String name);
	}
	AdapterCallBack mActionCallBack;
	public void setAdatperCallBack(AdapterCallBack actionCallBack){
		mActionCallBack = actionCallBack;
	}
}
